<template>
	<view class="classlist">
		<!-- 如果首次加载数据 显示-->
		<view class="loadingLayout" v-if="!classList.length && !noData">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator :url="'/pages/preview/preview?id=' + item._id" class="item" v-for="(item, index) in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<!-- 底部显示加载更多，和加载完成 -->
		<view class="loadMore" v-if="classList.length || noData">
			<uni-load-more :status="noData ? 'noMore' : 'loading'"></uni-load-more>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import { onReachBottom } from '@dcloudio/uni-app';
import { apiGetClassList, apiGetHistoryList } from '@/api/apis.js';
import { gotoHome } from '@/utils/common.js';

const classList = ref([]);

const noData = ref(false);

//请求入参
const queryParmas = {
	pageNum: 1,
	pageSize: 12
};

let pageName;

onLoad((e) => {
	//解构接受参数
	let { id = null, name = null, type = null } = e;
	if (type) queryParmas.type = type;
	if (id) queryParmas.classid = id;
	//参数为空，返回首页
	// if(!id){
	// 	gotoHome()
	// 	return;
	// }
	pageName = name;

	//设置标题栏
	uni.setNavigationBarTitle({
		title: name
	});
	//请求数据
	getClassList();
});

//页面销毁时清除缓存数据
onUnload(() => {
	uni.removeStorageSync('storageClassList');
});

//触底加载更多
onReachBottom(() => {
	if (noData.value) return;
	queryParmas.pageNum++;
	getClassList();
});

//下拉刷新
onPullDownRefresh(() => {
	console.log('onPullDownRefresh');
	queryParmas.pageNum = 1;
	noData.value = false;
	classList.value = [];
	getClassList();
});

//获取专题精选分类
const getClassList = async () => {
	uni.stopPullDownRefresh();
	let res;
	if (queryParmas.classid) {
		res = await apiGetClassList(queryParmas);
	}
	if (queryParmas.type) {
		res = await apiGetHistoryList(queryParmas);
	}
	classList.value = [...classList.value, ...res.data];
	//是否还有更多数据
	if (res.data.length < queryParmas.pageSize) {
		noData.value = true;
	}
	uni.setStorageSync('storageClassList', classList.value);
};

//分享朋友
onShareAppMessage((res) => {
	console.log(res);
	return {
		title: '谊康壁纸-' + pageName,
		path: 'pages/classlist/classlist?id=' + queryParmas.classid + '&name=' + pageName
	};
});

//分享朋友圈
onShareTimeline(() => {
	return {
		title: '谊康壁纸-' + pageName,
		query: 'id=' + queryParams.classid + '&name=' + pageName
	};
});
</script>

<style lang="scss" scoped>
.classlist {
	.content {
		display: grid;
		gap: 5rpx;
		grid-template-columns: repeat(3, 1fr);
		padding: 5rpx;
		.item {
			height: 440rpx;
			image {
				width: 100%;
				height: 100%;
				display: block;
			}
		}
	}
}
</style>
